using Newtonsoft.Json;
using System.Collections.Generic;

namespace BanZuiCloudControlAgent.Core.Mqtt.Models
{
    /// <summary>
    /// 断开连接消息负载模型
    /// </summary>
    public class DisconnectPayloadModel
    {
        /// <summary>
        /// 断开原因
        /// </summary>
        [JsonProperty("reason")]
        public string Reason { get; set; }

        /// <summary>
        /// 最后状态
        /// </summary>
        [JsonProperty("lastStatus")]
        public Dictionary<string, object> LastStatus { get; set; }
    }

    /// <summary>
    /// 断开连接消息模型
    /// </summary>
    public class DisconnectModel : BaseMessageModel
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public DisconnectModel() : base()
        {
            MessageType = MessageTypeConstants.DISCONNECT;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="payload">断开连接负载</param>
        public DisconnectModel(string deviceCode, DisconnectPayloadModel payload)
            : base(MessageTypeConstants.DISCONNECT, deviceCode, payload)
        {
        }

        /// <summary>
        /// 获取断开连接负载
        /// </summary>
        /// <returns>断开连接负载</returns>
        public DisconnectPayloadModel GetPayload()
        {
            return Payload as DisconnectPayloadModel;
        }

        /// <summary>
        /// 创建意外断开连接消息
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <returns>断开连接消息模型</returns>
        public static DisconnectModel CreateUnexpectedDisconnect(string deviceCode)
        {
            var payload = new DisconnectPayloadModel
            {
                Reason = "unexpected_disconnect",
                LastStatus = new Dictionary<string, object>
                {
                    { "memoryUsage", GetMemoryUsage() },
                    { "cpuUsage", GetCpuUsage() }
                }
            };

            return new DisconnectModel(deviceCode, payload);
        }

        /// <summary>
        /// 创建正常断开连接消息
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <returns>断开连接消息模型</returns>
        public static DisconnectModel CreateNormalDisconnect(string deviceCode)
        {
            var payload = new DisconnectPayloadModel
            {
                Reason = "normal_disconnect",
                LastStatus = new Dictionary<string, object>
                {
                    { "memoryUsage", GetMemoryUsage() },
                    { "cpuUsage", GetCpuUsage() }
                }
            };

            return new DisconnectModel(deviceCode, payload);
        }

        /// <summary>
        /// 创建错误断开连接消息
        /// </summary>
        /// <param name="deviceCode">设备码</param>
        /// <param name="errorMessage">错误信息</param>
        /// <returns>断开连接消息模型</returns>
        public static DisconnectModel CreateErrorDisconnect(string deviceCode, string errorMessage)
        {
            var payload = new DisconnectPayloadModel
            {
                Reason = "error_disconnect",
                LastStatus = new Dictionary<string, object>
                {
                    { "memoryUsage", GetMemoryUsage() },
                    { "cpuUsage", GetCpuUsage() },
                    { "errorMessage", errorMessage }
                }
            };

            return new DisconnectModel(deviceCode, payload);
        }

        #region 辅助方法

        private static double GetMemoryUsage()
        {
            // TODO: 实现获取内存使用率的方法
            return 60.0;
        }

        private static double GetCpuUsage()
        {
            // TODO: 实现获取CPU使用率的方法
            return 30.5;
        }

        #endregion
    }
} 